<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col>
        <!--好友申请数据-->
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="80px" v-show="showSearch" >
          <el-form-item label="好友账号" prop="loginName">
            <el-input v-model="queryParams.loginName" placeholder="请输入好友账号" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item label="好友姓名" prop="realName">
            <el-input v-model="queryParams.realName" placeholder="请输入好友姓名" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item label="手机号码" prop="mobileNumber">
            <el-input v-model="queryParams.mobileNumber" placeholder="请输入手机号码" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item label="处理状态" prop="applyStatus">
            <el-select @change="handleQuery" v-model="queryParams.applyStatus" size="small" clearable style="width: 150px" placeholder="请选择">
              <el-option key="" label="全部" value=""/>
              <el-option key="0" label="待处理" value="0"/>
              <el-option key="1" label="已同意" value="1"/>
              <el-option key="2" label="已拒绝" value="2"/>
            </el-select>
          </el-form-item>
          <el-form-item label="申请时间">
            <el-date-picker @change="handleQuery" v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary"  size="mini" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery('queryForm')">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
        <!--  <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="saveChatUsersFriendsApply">新增</el-button>
          </el-col>-->
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-check" size="mini" :disabled="single"  @click="acceptChatUsersFriendsApply">同意</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-close" size="mini" :disabled="single"  @click="declineChatUsersFriendsApply">拒绝</el-button>
          </el-col>

          <right-toolbar :showSearch.sync="showSearch" @queryTable="queryMyChatUsersFriendsApplyPageResList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table ref="multipleTable" :stripe="true" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange"
                  @sort-change='tableSortChange' :default-sort="{prop: 'sort', order: 'descending'}">
          <el-table-column type="selection" align="center" :selectable="checkSelectable"/>
          <!--<el-table-column type="index"/>-->
          <el-table-column label="头像" align="center" prop="headPortrait" width="100">
            <template slot-scope="scope">
              <img width="40" height="40" :src="scope.row.headPortrait" :onerror="$store.state.defaultAvatar"/>
            </template>
          </el-table-column>
          <el-table-column label="好友账号" align="left" prop="loginName"   width="150"/>
          <el-table-column label="好友姓名" align="left" prop="realName" width="150"/>
          <el-table-column label="手机号码" align="center" prop="mobileNumber"  width="150" />
          <el-table-column label="申请信息" align="center" prop="applyDescription"/>
          <el-table-column label="申请时间" align="center" prop="createTime" sortable='custom' width="200" />
          <el-table-column label="处理状态" align="center" prop="applyStatus"  width="150">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.applyStatus===1">已同意</el-tag>
              <el-tag type="warning" v-if="scope.row.applyStatus===2">已拒绝</el-tag>
              <el-tag type="info" v-if="scope.row.applyStatus===0">待处理</el-tag>
            </template>
          </el-table-column>

          <el-table-column label="处理时间" align="center" prop="handleTime" sortable='custom'  width="200" >
            <template slot-scope="scope">
              <span v-if="scope.row.applyStatus!==0" v-html="scope.row.handleTime"></span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" v-if="scope.row.applyStatus===0" icon="el-icon-check"  @click="acceptChatUsersFriendsApply(scope.row)">同意</el-button>
              <el-button size="mini" type="text" v-if="scope.row.applyStatus===0" icon="el-icon-close"  @click="declineChatUsersFriendsApply(scope.row)">拒绝</el-button>
              <el-button size="mini" type="text" icon="el-icon-message" v-if="scope.row.applyStatus===1"  @click="toTalkPage(scope.row)">发送消息</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.rows"
                    @pagination="queryMyChatUsersFriendsApplyPageResList"/>
      </el-col>
    </el-row>

    <!-- 用户查询 -->
    <UserSearch ref="searchUsers" />
  </div>
</template>
<script>
  import UserSearch from '@/components/DuoJuHe/Chat/user/UserSearch'
  import {queryMyChatUsersFriendsApplyPageResList, acceptChatUsersFriendsApply, declineChatUsersFriendsApply} from '@/api/chat/im/chatUsersFriendsApply'
  import { toTalk } from '@/utils/talk'
  //默认查询参数
  let defaultQueryParams = {
    loginName: undefined,
    realName: undefined,
    startTime: undefined,
    endTime: undefined,
    mobileNumber: undefined,
    applyStatus: "",
    page: 1,
    rows: 10
  }
  export default {
    name: "myChatFriendsApplyList",
    components: {
      UserSearch
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 日期范围
        dateRange: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 显示搜索条件
        showSearch: true,
        // table表格数据
        dataList: [],
        // 查询参数
        queryParams: { ...defaultQueryParams },
        //列信息
        columns: []
      }
    },
    created() {
      this.queryMyChatUsersFriendsApplyPageResList();
    },
    methods: {
      /** 查询好友申请列表 */
      queryMyChatUsersFriendsApplyPageResList() {
        this.loading = true
        queryMyChatUsersFriendsApplyPageResList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.dataList = response.data.records
          this.total = response.data.total
          this.loading = false
        })
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.applyId)
        this.single = selection.length !== 1;
        this.multiple = !selection.length
      },
      //排序
      tableSortChange(column){
        //格式化排序参数，构建中
        this.orderByFormat(this.queryParams,column);
        //执行查询方法
        this.queryMyChatUsersFriendsApplyPageResList()
      },
      /** 同意 */
      acceptChatUsersFriendsApply(row) {
        row = row.isTrusted ? this.$refs.multipleTable.selection[0] : row;
        const applyId = row.applyId;
        this.$confirm('确认同意【'+row.realName+'】好友申请?', "警告", {
          cancelButtonText: "取消",
          confirmButtonText: "确定",
          type: "warning"
        }).then(function() {
          const applyReq={applyId:applyId,remark:"同意"}
          return acceptChatUsersFriendsApply(applyReq);
        }).then(response => {
          this.queryMyChatUsersFriendsApplyPageResList();
          this.$modal.msgSuccess(response.message)
        }).catch(function() {});
      },

      /** 拒绝 */
      declineChatUsersFriendsApply(row) {
        row = row.isTrusted ? this.$refs.multipleTable.selection[0] : row;
        const applyId = row.applyId;
        this.$confirm('确认拒绝【'+row.realName+'】好友申请?', "警告", {
          cancelButtonText: "取消",
          confirmButtonText: "确定",
          type: "warning"
        }).then(function() {
          const applyReq={applyId:applyId,remark:"拒绝"}
          return declineChatUsersFriendsApply(applyReq);
        }).then(response => {
          this.queryMyChatUsersFriendsApplyPageResList();
          this.$modal.msgSuccess(response.message)
        }).catch(function() {});
      },

      // 跳转聊天页面
      toTalkPage(row) {
        toTalk(1, row.userId)
      },
      // 添加好友
      saveChatUsersFriendsApply() {
        this.$refs.searchUsers.open()
      },
      // 是否允许选择
      checkSelectable(row) {
        return row.applyStatus===0
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1
        this.queryMyChatUsersFriendsApplyPageResList()
      },
      /** 重置按钮操作 */
      resetQuery(fromId) {
        this.dateRange = [];
        this.resetForm(fromId);
        this.handleQuery()
      }
    }
  }
</script>
